<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>数据类型 | Oliver知识收集站</title>
    <meta name="generator" content="VuePress 1.9.7">
    
    <meta name="description" content="享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css" as="style"><link rel="preload" href="/oliver-vuepress/assets/js/app.c21e6ffc.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/3.6dd9a2a1.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/1.898920d0.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/20.268bd174.js" as="script"><link rel="prefetch" href="/oliver-vuepress/assets/js/10.41b2bf91.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/11.a95c117d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/12.8607f0e1.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/13.a52d6846.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/14.249b4e52.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/15.d458d12e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/16.ba334206.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/17.1b91c9fa.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/18.e2ea2eb5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/19.bf0e2553.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/21.cd1bbed5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/22.da4bc7f7.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/23.12f0c72f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/24.b7886742.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/25.6e71af85.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/26.5c127243.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/27.e98fd8bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/28.ce83b09c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/29.50398f0f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/30.05e1339c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/31.ef4b13fb.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/32.ba5f8351.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/33.3902db0a.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/34.36a05884.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/35.87215872.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/36.db360c58.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/37.402e5374.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/38.c9228dd8.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/39.72ba5d1f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/4.7bb03d47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/40.7e7949bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/41.c0d5b947.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/42.d9984467.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/43.e6a43668.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/44.10d7fe47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/45.f692ec2d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/46.9b920343.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/47.8e3d94f9.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/48.7d356e5b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/49.b0df6271.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/5.1fa544da.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/50.805e1466.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/51.1b31d40e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/52.44e69a41.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/53.da1def53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/54.6569f7db.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/55.5fc3de47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/56.da649377.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/57.6ff15ed4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/58.a62f6424.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/59.f68ae517.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/6.f5bd8e9b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/60.dda416bc.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/61.4e0c719f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/62.8c5ef01e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/63.7089eb8b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/64.b5ec150d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/65.6720cda4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/66.4ee90e29.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/67.cc4b0c6d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/7.d5950c53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/8.382fb3a5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/9.d593f4c1.js">
    <link rel="stylesheet" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Oliver知识收集站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/oliver-vuepress/" class="home-link router-link-active"><!----> <span class="site-name">Oliver知识收集站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link router-link-active"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><!----> <h3 class="name" data-v-39576ba9>
    oliver.shi
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>52</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link router-link-active"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础篇</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/oliver-vuepress/articles/java/basics/oop.html" class="sidebar-link">面向对象</a></li><li><a href="/oliver-vuepress/articles/java/basics/enum.html" class="sidebar-link">枚举</a></li><li><a href="/oliver-vuepress/articles/java/basics/datatype.html" aria-current="page" class="active sidebar-link">数据类型</a></li><li><a href="/oliver-vuepress/articles/java/basics/string.html" class="sidebar-link">字符串</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>数据类型</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">数据类型</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>oliver.shi</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>11/7/2020</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Java</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="数据类型分类"><a href="#数据类型分类" class="header-anchor">#</a> 数据类型分类</h2> <ul><li><p>值类型</p></li> <li><p>引用类型 （简单地讲，“引用”是存储在有序的内存栈上的，而对象本身的值存储在内存堆上的；）</p></li></ul> <h3 id="值类型"><a href="#值类型" class="header-anchor">#</a> 值类型</h3> <p>Java 提供了8中基本类型，4大类类型</p> <ul><li>整形
<ul><li>byte (存储为8位，取值范围[2^7, 2^7-1],即[-128, 127])</li> <li>short (存储单位16位)</li> <li>int (存储单位为32位)</li> <li>long (存储单位为64位,赋值时一般在数字后加上 l 或 L)</li></ul></li> <li>浮点型
<ul><li>float (存储单位为32位, 赋值时必须在数字后加上 f 或 F)</li> <li>double (存储单位为64位,赋值时一般在数字后加 d 或 D)</li></ul></li> <li>布尔型
<ul><li>boolean  (存储单位为1位)</li></ul></li> <li>字符型
<ul><li>char (存储单位为16位)</li></ul></li></ul> <h3 id="值类型-和-引用类型"><a href="#值类型-和-引用类型" class="header-anchor">#</a> 值类型 和 引用类型</h3> <ul><li>从概念来说
<ul><li>基本类型：变量名指向具体数值</li> <li>引用类型：变量名指向数据对象的内存地址</li></ul></li> <li>从内存方面来说
<ul><li>基本类型：变量在声明之后，Java 就会立刻分配给他内存空间。</li> <li>引用类型：它以特殊的方式（类似 C 指针）向对象实体（具体的值），这类变量声明时不会分配内存，只是存储了一个内存地址。</li></ul></li> <li>从使用方面来说
<ul><li>基本类型：使用时需要赋具体值,判断时使用 == 号。</li> <li>引用类型：使用时可以赋 null，判断时使用 equals 方法。</li></ul></li></ul> <h3 id="引用类型分为四类"><a href="#引用类型分为四类" class="header-anchor">#</a> 引用类型分为四类</h3> <p><strong>强引用</strong></p> <p>垃圾回收器：</p> <p>内存充足时，不会回收</p> <p>当内存不足时，也不会回收，Java虚拟机会抛出 <code>OutOfMemoryError</code></p> <p>当对 强以引用赋 null 时，jvm 会在适当的时间进行回收</p> <p><strong>软引用</strong></p> <ul><li>当内存足够时，不会回收</li> <li>当内容不足时，会进行回收</li> <li>用java.lang.ref.SoftReference类来表示软引用。</li></ul> <p><strong>弱引用</strong></p> <ul><li>无论内存是否足够，只要JVM开始进行垃圾回收，都会被回收</li> <li>用 java.lang.ref.WeakReference 来表示弱引用</li> <li>经典案例  ThreadLocal 是个虚引用</li></ul> <p><strong>引虚用</strong></p> <ul><li>一个对象仅持有虚引用，那么它就和没有任何引用一样，它随时可能会被回收</li> <li>用 PhantomReference类表示虚引用</li> <li>类中只有一个 构造方法 和 get() 方法 且仅仅返回一个null</li> <li>虚引用 必须 要和 ReferenceQueue 引用队列一起使用。</li></ul> <h2 id="数据类型转换"><a href="#数据类型转换" class="header-anchor">#</a> 数据类型转换</h2> <p>java 中有两种数据转换方式</p> <ul><li>自动转换</li> <li>强制转换</li></ul> <h3 id="自动转换"><a href="#自动转换" class="header-anchor">#</a> 自动转换</h3> <ul><li>由小数据转为大数据 （比如数据计算）</li> <li>转换前后数据类型需要兼容</li> <li>整型类型和浮点型进行计算后，结果会转为浮点型</li></ul> <h3 id="强制转换"><a href="#强制转换" class="header-anchor">#</a> 强制转换</h3> <p>有些时候不满足，自动转换的标准时，会进行一些手动转换<br>
强制转换使用括号()<br>
强制转转换 可能会丢失进度</p> <div class="language- extra-class"><pre class="language-text"><code>float f = 3.55f
int x = (int) f
</code></pre></div><h2 id="装箱、拆箱"><a href="#装箱、拆箱" class="header-anchor">#</a> 装箱、拆箱</h2> <h3 id="基本数据类型-与-包装类关系"><a href="#基本数据类型-与-包装类关系" class="header-anchor">#</a> 基本数据类型 与 包装类关系</h3> <p>包装类：都是对象</p> <p>java中为每一种基本数据类型提供了相应的包装类，如下：</p> <table><thead><tr><th>基本数据类型</th> <th>包装类</th></tr></thead> <tbody><tr><td>byte</td> <td>Byte</td></tr> <tr><td>short</td> <td>Short</td></tr> <tr><td>int</td> <td>Integer</td></tr> <tr><td>long</td> <td>Long</td></tr> <tr><td>float</td> <td>Float</td></tr> <tr><td>double</td> <td>Double</td></tr> <tr><td>boolean</td> <td>Boolean</td></tr> <tr><td>char</td> <td>Character</td></tr></tbody></table> <h3 id="装箱、拆箱-2"><a href="#装箱、拆箱-2" class="header-anchor">#</a> 装箱、拆箱</h3> <ul><li>装箱（boxing）是将值类型转换为引用类型。例如：int 转 Integer
<ul><li>装箱过程是通过调用包装类的 valueOf 方法实现的。</li></ul></li> <li>拆箱（unboxing）是将引用类型转换为值类型。例如：Integer 转 int
<ul><li>拆箱过程是通过调用包装类的 xxxValue 方法实现的。（xxx 代表对应的基本数据类型）。</li></ul></li></ul> <h3 id="自动装箱、自动拆箱"><a href="#自动装箱、自动拆箱" class="header-anchor">#</a> 自动装箱、自动拆箱</h3> <p>自在JDK5开始提供了 自动装箱，拆箱；
自动装箱与拆箱机制，是为了我们在赋值或者方法调用的时候使用基本数据类型和对象类型更加简单直接</p> <p><strong>那些场景会自动拆装箱</strong></p> <ul><li>基本类型放入集合类</li> <li>包装类 与 基本类型大小比较</li> <li>包装类的数值运算</li> <li>三目运算</li> <li>函数的参数 和 返回值</li></ul> <h3 id="自动拆装箱-提一部分的缓存机制"><a href="#自动拆装箱-提一部分的缓存机制" class="header-anchor">#</a> 自动拆装箱 提一部分的缓存机制</h3> <blockquote><p>java 5 中，在 Integer 的操作上引入了一个新功能来节省内存和提高性能。整型对象通过使用相同的对象引用实现缓存和重用。</p></blockquote> <ul><li>适用于整数值区间 -128 至 +127。</li> <li>最大值 127 可以通过 -XX:AutoBoxCacheMax=size 修改</li> <li>在 Java 6 中，可以通过 java.lang.Integer.IntegerCache.high 设置最大值</li></ul> <p>通过valueOf 方法 我们可以直接  整数值区间 -128 至 +127 会通过 <code>IntegerCache</code> 获取</p> <div class="language-java extra-class"><pre class="language-java"><code>    <span class="token comment">/**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Integer</span> <span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">&gt;=</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>low <span class="token operator">&amp;&amp;</span> i <span class="token operator">&lt;=</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>high<span class="token punctuation">)</span>
            <span class="token keyword">return</span> <span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>cache<span class="token punctuation">[</span>i <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token operator">-</span><span class="token class-name">IntegerCache</span><span class="token punctuation">.</span>low<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Integer</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

</code></pre></div><p><strong>所以在这个范围内可以通过  == 进行判断，超过则不不行， 所有我们建议都 equals 进行相等判断</strong></p> <p>其他的缓存对象：</p> <ul><li>有ByteCache用于缓存Byte对象</li> <li>有ShortCache用于缓存Short对象</li> <li>有LongCache用于缓存Long对象</li> <li>有CharacterCache用于缓存Character对象</li></ul> <p>其中：</p> <ul><li><code>Byte</code>, <code>Short</code>, <code>Long</code> 有固定范围: -128 到 127。</li> <li>对于 <code>Character</code>, 范围是 0 到 127</li></ul> <blockquote><p>除了 <code>Integer</code> 以外，这个范围都不能改变。</p></blockquote> <h2 id="数值计算"><a href="#数值计算" class="header-anchor">#</a> 数值计算</h2> <ul><li>浮点数无法紧缺表达和运算的场景，一定要使用 <code>BigDecimal</code> 类型</li> <li>浮点数的字符串格式化也要通过 BigDecimal 进行</li> <li>BigDecimal 可以使用 <code>compareTo</code> 方法比较大小</li> <li>数值溢出，当数值计算-或转换溢出，是默默地溢出，并不会抛出任何异常</li></ul> <blockquote><p>可以参考一下文章：</p> <ul><li>https://juejin.im/post/6844903501256998926</li> <li>https://juejin.im/post/6844903545464946695</li> <li>https://www.hollischuang.com/archives/1174</li></ul></blockquote></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">4/21/2022, 8:17:59 AM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/oliver-vuepress/articles/java/basics/enum.html" class="prev">
            枚举
          </a></span> <span class="next"><a href="/oliver-vuepress/articles/java/basics/string.html">
            字符串
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#数据类型分类" class="sidebar-link reco-side-数据类型分类" data-v-cb1513f6>数据类型分类</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#值类型" class="sidebar-link reco-side-值类型" data-v-cb1513f6>值类型</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#值类型-和-引用类型" class="sidebar-link reco-side-值类型-和-引用类型" data-v-cb1513f6>值类型 和 引用类型</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#引用类型分为四类" class="sidebar-link reco-side-引用类型分为四类" data-v-cb1513f6>引用类型分为四类</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#数据类型转换" class="sidebar-link reco-side-数据类型转换" data-v-cb1513f6>数据类型转换</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#自动转换" class="sidebar-link reco-side-自动转换" data-v-cb1513f6>自动转换</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#强制转换" class="sidebar-link reco-side-强制转换" data-v-cb1513f6>强制转换</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#装箱、拆箱" class="sidebar-link reco-side-装箱、拆箱" data-v-cb1513f6>装箱、拆箱</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#基本数据类型-与-包装类关系" class="sidebar-link reco-side-基本数据类型-与-包装类关系" data-v-cb1513f6>基本数据类型 与 包装类关系</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#装箱、拆箱-2" class="sidebar-link reco-side-装箱、拆箱-2" data-v-cb1513f6>装箱、拆箱</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#自动装箱、自动拆箱" class="sidebar-link reco-side-自动装箱、自动拆箱" data-v-cb1513f6>自动装箱、自动拆箱</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#自动拆装箱-提一部分的缓存机制" class="sidebar-link reco-side-自动拆装箱-提一部分的缓存机制" data-v-cb1513f6>自动拆装箱 提一部分的缓存机制</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/datatype.html#数值计算" class="sidebar-link reco-side-数值计算" data-v-cb1513f6>数值计算</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/oliver-vuepress/assets/js/app.c21e6ffc.js" defer></script><script src="/oliver-vuepress/assets/js/3.6dd9a2a1.js" defer></script><script src="/oliver-vuepress/assets/js/1.898920d0.js" defer></script><script src="/oliver-vuepress/assets/js/20.268bd174.js" defer></script>
  </body>
</html>
